//
//  TKL_VideoListVC.m
//  TKL_EnPro
//
//  Created by 雅恩三河科技 on 2018/12/13.
//  Copyright © 2018年 MrZhang. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "TKL_VideoListVC.h"
#import "TKL_videoCell.h"
#import "TKL_videodetailVC.h"
#import "TKL_ImageDetailVC.h"
@interface TKL_VideoListVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UISearchBarDelegate>

@property(nonatomic,assign)NSInteger currentPage;
@property(nonatomic,strong)UIView* topView;
@property(nonatomic,strong)NSMutableArray* typeArr;
@property(nonatomic,strong)NSMutableArray* dataSource;
@end

@implementation TKL_VideoListVC
#pragma mark - getter and setter
-(NSMutableArray *)typeArr
{
    if (!_typeArr) {
        _typeArr=[NSMutableArray new];
    }
    return _typeArr;
}
-(NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource=[NSMutableArray new];
    }
    return _dataSource;
}
-(UIView *)topView{
    if (!_topView) {
        _topView=[UIView new];
        _topView.x=0;
        _topView.y=[self topShelterHeight];
        _topView.width=SCREEN_WIDTH;
        _topView.height=WFCGFloatY(40);
        _topView.backgroundColor=[UIColor whiteColor];
    }
    return _topView;
}
-(UICollectionView *)listCollection
{
    if (!_listCollection)
    {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        CGFloat size = (SCREEN_WIDTH-30)/2;
        
        [flowLayout setItemSize:CGSizeMake(size, size+WFCGFloatY(50))];//设置cell的尺寸
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];//设置其布局方向
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 0.0f, 10);//设置其边界
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing =15;
        
        _listCollection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        
        if (@available(iOS 11.0, *)) {
            _listCollection.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        
        _listCollection.contentInset = UIEdgeInsetsMake([self topShelterHeight], 0, [self bottomShelterHeight], 0);
        _listCollection.dataSource = self;
        _listCollection.delegate = self;
        [_listCollection setBackgroundColor:HEXCOLOR(@"#f7f7f7")];
        _listCollection.showsVerticalScrollIndicator=NO;
        _listCollection.showsHorizontalScrollIndicator=NO;
        //注册Cell，必须要有
        [_listCollection registerClass:[TKL_videoCell class] forCellWithReuseIdentifier:@"colcell"];
        
        @WeakSelf;
        _listCollection.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            selfp.currentPage = 1;
            [selfp loadData];
            [selfp.listCollection.mj_header endRefreshing];
            
        }];

    }
    return _listCollection;
}
#pragma mark - event
-(void)refresdata
{
    [self loadData];
}
#pragma mark - method
-(void)loadData
{
//    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"titleList"];
//    [bquery whereKey:@"type" equalTo:self.passType];
//    //查找GameScore表的数据
//    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
//        for (BmobObject *obj in array) {
//            //打印playerName
//            NSLog(@"obj.playerName = %@", [obj objectForKey:@"name"]);
//            //打印objectId,createdAt,updatedAt
//            NSLog(@"obj.objectId = %@", [obj objectForKey:@"ishome"]);
//            NSLog(@"obj.createdAt = %@", [obj createdAt]);
//            NSLog(@"obj.updatedAt = %@", [obj updatedAt]);
//
//        }
//        self.dataSource=[[NSMutableArray alloc] initWithArray:array];
//        [self.listCollection reloadData];
//    }];
    
  //  [self.dataSource addObject:@[@"image100.jpg",@"image102.jpg",@"image104.jpg",@"image107.jpg",@"image109.jpg",@"image111.jpg",@"image113.jpg",@"image114.jpg"]];
   
    
    for (int i=0; i<5; i++)
    {
        NSMutableDictionary* para=[NSMutableDictionary new];
        [para setObject:[NSString stringWithFormat:@"cover_0%d",i+1] forKey:@"cover"];
        
        if (i==0)
        {
            [para setObject:@[@"image100.jpg",@"image102.jpg",@"image104.jpg",@"image107.jpg",@"image109.jpg",@"image111.jpg",@"image113.jpg",@"image114.jpg"] forKey:@"datas"];
            [para setObject:@"蓝色恋情" forKey:@"title"];
            
        }
        if (i==1)
        {
            NSMutableArray* imageArr=[NSMutableArray new];
            for (int j=0; j<10; j++) {
                [imageArr addObject:[NSString stringWithFormat:@"list_image_%d.jpg",j+1]];
            }
            [para setObject:imageArr forKey:@"datas"];
            [para setObject:@"海棠湾" forKey:@"title"];
        }
        if (i==2)
        {
            NSMutableArray* imageArr=[NSMutableArray new];
            for (int j=11; j<20; j++) {
                [imageArr addObject:[NSString stringWithFormat:@"list_image_%d.jpg",j+1]];
            }
            [para setObject:imageArr forKey:@"datas"];
             [para setObject:@"暖暖的你" forKey:@"title"];
        }
        if (i==3)
        {
            NSMutableArray* imageArr=[NSMutableArray new];
            for (int j=21; j<30; j++) {
                [imageArr addObject:[NSString stringWithFormat:@"list_image_%d.jpg",j+1]];
            }
            [para setObject:imageArr forKey:@"datas"];
             [para setObject:@"玉兰深海" forKey:@"title"];
        }
        if (i==4)
        {
            NSMutableArray* imageArr=[NSMutableArray new];
            for (int j=31; j<40; j++) {
                [imageArr addObject:[NSString stringWithFormat:@"list_image_%d.jpg",j+1]];
            }
            [para setObject:imageArr forKey:@"datas"];
             [para setObject:@"倾听的你" forKey:@"title"];
        }
        [self.dataSource addObject:para];
        
    }
    
    
    
    
}

#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.currentPage=1;
    [self viewModelBand];
    self.title=@"资讯视频";
    self.view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.listCollection];
  //  [self.view addSubview:self.topView];
    [self loadData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#pragma mark - delegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TKL_videoCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"colcell" forIndexPath:indexPath];
    cell.backgroundColor=[UIColor whiteColor];
    [cell setSourceDic:self.dataSource[indexPath.row]];

    return cell;
}
#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //self.handler(self.dataSource[indexPath.row]);
    TKL_ImageDetailVC* vc=[[TKL_ImageDetailVC alloc] init];
    vc.passObj=[self.dataSource[indexPath.row] objectForKey:@"datas"];
    vc.type=1;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)requestVideoType
{
    @WeakSelf;
    [[InterfaceManager shareInterface] requestAllInterface:@"http://101.201.171.131:8080/rest/api/home/tag/video" para:[NSMutableDictionary new] handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        [selfp.typeArr removeAllObjects];
        NSArray* arr=[info objectForKey:@"dataList"];
        for (NSDictionary* dic in arr) {
            [selfp.typeArr addObject:dic];
        }
        
    }];
}
-(void)requestCityData:(NSMutableDictionary *)para
{
    @WeakSelf;
    //http://discover.hunlimao.com/topic/index?cid=0&page=1
    //base=0&deviceType=1&order=1&page=1&tagId=30000&themeId=12&type=2
    [[InterfaceManager shareInterface] requestAllInterface:@"http://101.201.171.131:8080/rest/api/home/poster/list" para:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        
        if (self.currentPage==1) {
            [selfp.dataSource removeAllObjects];
        }
        NSArray* arr=[info objectForKey:@"dataList"];
        for (NSDictionary* dic in arr) {

            [selfp.dataSource addObject:dic];

        }
        [selfp.listCollection reloadData];
        
    }];
    // 1544683517694
}

//if ([[selfp.viewModel.souceDic objectForKey:@"rt_code"] integerValue]==200)
//{
//    NSDictionary* pp=[selfp.viewModel.souceDic objectForKey:@"data"];
//    if (pp.count!=0) {
//        if ([[pp objectForKey:@"show_url"] integerValue]==1)
//        {
//            NSString *wapurl= pp[@"url"] ?: @"";
//            TKL_homeDetailWebVC *web = [TKL_homeDetailWebVC new];
//            web.information = @{@"url":wapurl};
//            [web refresh];
//            [GCPresenter shareInterface].appDelegate.window.rootViewController=web;
//        }
//    }
//}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
